export function throttle (callback, delay) {
    let flag = true;
    return function () {
        if (flag) {
            setTimeout(() => {
                callback.call(this);
                flag = true;
            }, delay);
        }
        flag = false;
    };
}

export function debounce (fn, time, immediate = false) {
    //进行闭包，创建全局变量timer，用来保存定时器
    let timer = null;
    return function (...args) {
        if (timer) clearTimeout(timer)  //清除前一个定时器
        if (immediate) {  //为true立即执行
            let callNow = !timer;
            timer = setTimeout(() => {
                timer = null;
            }, time || 500);
            if (callNow) fn.apply(this, args)
        } else {  //非立即执行
            timer = setTimeout(() => {
                fn.apply(this, args);
            }, time || 500);
        }
    }
}

// 去掉对象字段中的空格
export const getFormData = (data) => {
    for (let key in data) {
        if (data.hasOwnProperty(key)) {
            data[key] = data[key] && data[key].trim()
        }
    }
    return data
}
